package com.ygxsk.carhome.retrofit;

import android.text.TextUtils;

import com.ygxsk.carhome.constant.ConstantSign;
import com.ygxsk.carhome.response.HttpEntityProperty;

import org.jetbrains.annotations.NotNull;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableSource;
import io.reactivex.rxjava3.core.ObservableTransformer;
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.schedulers.Schedulers;

/*
 *仅仅是上传日志用到，单独的，因为日志有死循环，一直报错，一直提交，导致传递给后台很多斜杠
 */
public class ResponseTransformAddLog<SucceedData> implements ObservableTransformer<HttpEntityProperty<SucceedData>, SucceedData> {


    protected AddLogUpLoadInfo addLogUpLoadInfo;

    /**
     * 每个界面之中的类似Handler的监听器。
     */
    protected ApiRequestSubListener<SucceedData> apiRequestSubListener;


    public ResponseTransformAddLog(AddLogUpLoadInfo _addLogUpLoadInfo,//
                                   ApiRequestSubListener<SucceedData> _apiRequestSubListener) {
        this.addLogUpLoadInfo = _addLogUpLoadInfo;
        this.apiRequestSubListener = _apiRequestSubListener;
    }

    @NotNull
    @Override
    public ObservableSource<SucceedData> apply(@NotNull Observable<HttpEntityProperty<SucceedData>> upstream) {

        return upstream.onErrorResumeNext(new Function<Throwable, ObservableSource<? extends HttpEntityProperty<SucceedData>>>() {

                    @Override
                    public ObservableSource<? extends HttpEntityProperty<SucceedData>> apply(@NotNull Throwable throwable) throws Exception {

//                        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "--------onErrorResumeNext---------apply------>执行的是-->" + addLogUpLoadInfo.getUrlPath());

//                        if (throwable != null) {
                        //这个日志不完整
                        //LogUtils.d(ConstantApi.LOGCAT_NOMAL + "ResponseTransformer请求中间报错-->" + throwable.getLocalizedMessage());

                        //打印报错的日志信息
//                            if (addLogUpLoadInfo != null) {
//                                LogUtils.d("itchenqi---接口名#" + addLogUpLoadInfo.getUrlPath() + "#---------Throwable--->" + throwable.getClass().getName() + "#" + throwable.getLocalizedMessage());
//                            }
//                        }

                        return Observable.error(new ApiException(//是一个代替，若以上出异常就走这里
                                ConstantSign.REQUEST_30003, //
                                throwable.getClass().getName() + "#" + throwable.getLocalizedMessage(),//不做数据响应的翻译
                                addLogUpLoadInfo,//
                                apiRequestSubListener));

                        //做正式环境的错误信息翻译
//                        return Observable.error(new ApiException(//是一个代替，若以上出异常就走这里
//                                ConstantSign.REQUEST_30003, //
//                                (ApiService.IS_DEBUG) ? throwable.getClass().getName() + "#" + throwable.getLocalizedMessage() : ConstantSign.REQUEST_30003_TEXT,//
//                                addLogUpLoadInfo,//
//                                apiRequestSubListener));

                    }

                }).flatMap(new Function<HttpEntityProperty<SucceedData>, ObservableSource<SucceedData>>() {//

                    @Override
                    public ObservableSource<SucceedData> apply(@NotNull HttpEntityProperty<SucceedData> responseData) throws Exception {

                        if (responseData != null) {
                            if (TextUtils.equals(ConstantSign.RESPONSE_200, responseData.getCode())) {//如果是200就是数据请求成功
                                if (responseData.getData() != null) {
                                    return Observable.just(responseData.getData());//
                                } else {
                                    return Observable.just(responseData.getData());
                                }
                            }//也就到200为止，
                        }
                        //请求异常,报错的code和报错的message
                        return Observable.error(new ApiException(//这是一个代替，如果以上 真的响应数据为空 就走这里，但是在AddLog日志接口内，不需要这个，可以抛弃
                                responseData.getCode(), //日志接口竟然没有msg和data和code
                                responseData.getMessage(),//JSON.toJSONString(responseData)
                                addLogUpLoadInfo,//
                                apiRequestSubListener));//
                    }
                }).subscribeOn(Schedulers.io())//维护有线程池
                .observeOn(AndroidSchedulers.mainThread());//主线程处理
    }
}

